//
//  CMQuestionCell.m
//  CreditManger
//
//  Created by sky on 15/11/9.
//  Copyright © 2015年 jinxin. All rights reserved.
//

#import "CMQuestionCell.h"
#import "CMGetQuestionAnswerInfo.h"

#define SPACE 15
#define ANSWERSPACE 20
#define STATICY line.origin.y + 20
#define ANSWERSTATICY _answerchose.origin.y + 2

@implementation CMQuestionCell{
    UIView *_bgvi;

    
    UILabel *_countlbl;
    UILabel *_tumlbl;
    UILabel *_answer;
    UIButton *_answerchose;
    
    NSArray *_answerarr;
    UIImageView *_conmarkvi;
}



-(void)setAlliteminfo:(CMGetQuestionItemInfo *)alliteminfo{
    _alliteminfo = alliteminfo;
    _answerarr = self.alliteminfo.answers;
    
    _bgvi.frame = CGRectMake(10, 2, SCREEN_WIDTH - 20, [self MakeHeightForViewWithShowString:self.alliteminfo.questionContent]);
    

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self.alliteminfo.questionContent];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:10];//调整行间距
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [self.alliteminfo.questionContent length])];
    _countlbl.attributedText = attributedString;
    [_countlbl sizeToFit];
    
    
    UIImage *green = [UIImage imageNamed:@"anwer_green"];
    UIImage *grow  = [UIImage imageNamed:@"anwer_grow"];
    UIImage *blue  = [UIImage imageNamed:@"anwer_blue"];
    
    if (self.alliteminfo.questionStatus == QuestionStatus_Open) {
        
        _conmarkvi.image = blue;
        
    }else if (self.alliteminfo.questionStatus == QuestionStatus_Close){
        
        _conmarkvi.image = grow;
        
    }else if (self.alliteminfo.questionStatus == QuestionStatus_Answer){
        
        _conmarkvi.image = green;
        
    }
    
    
}

-(void)setTum:(NSInteger)tum{
    
    _tum = tum;
    _tumlbl.text = [NSString stringWithFormat:@"%ld",(long)self.tum];
    
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        [self buildLayout];
    }
    return self;
}

-(void)buildLayout{

    _bgvi = [[UIView alloc]initWithFrame:CGRectMake(10, 2, SCREEN_WIDTH - 20, 0)];
    _bgvi.backgroundColor = [UIColor whiteColor];
    _bgvi.layer.cornerRadius = 2.5;
    _bgvi.layer.borderColor = [LCCommon getColor:@"cccccc"].CGColor;
    _bgvi.layer.borderWidth = .5;
    
    UIImage *jiaobiao = [UIImage imageNamed:@"anwer_green"];
    _conmarkvi = [[UIImageView alloc]initWithFrame:CGRectMake(10, 0 , jiaobiao.size.width, jiaobiao.size.height)];
    _tumlbl = [self LabelWithRect:CGRectMake(4, 4, 15,15) textColor:[UIColor whiteColor] textstr:@"" textfont:15 textstyle:NSTextAlignmentCenter];
    
    
    _countlbl = [self LabelWithRect:CGRectMake(20, 15, _bgvi.size.width - 35, 0) textColor:[LCCommon getColor:@"333333"] textstr:@"" textfont:16 textstyle:NSTextAlignmentLeft];
    
    
    [self.contentView addSubview:_bgvi];
    [_bgvi addSubview:_countlbl];
    [_conmarkvi addSubview:_tumlbl];
    [self.contentView addSubview:_conmarkvi];
}

- (void)awakeFromNib {
    // Initialization code
}


//-(void)choseCellWithStatus:(QuestionStatus)status{
//    if () {
//        
//    }
//}


-(CGFloat)MakeHeightForViewWithShowString:(NSString *)str{
    CGFloat height;
    CGSize strsize = [str sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}];
    NSInteger hang = strsize.width / (SCREEN_WIDTH - 20 - 35) + 1;
    if (self.alliteminfo.questionStatus == QuestionStatus_Close) {
        
        height = hang * 16 + 30 + (hang - 1) * 10;
        
    }else if(self.alliteminfo.questionStatus == QuestionStatus_Open){
        
        NSArray *arr = self.alliteminfo.answers;
        height = (hang * 16 + 30 + (hang - 1) * 10) + (40 + arr.count * 14 + (arr.count - 1) *22) ;
        [self makeAnswerChoseViewWithString:str];
        
    }else if (self.alliteminfo.questionStatus == QuestionStatus_Answer){
        
        height = hang * 16 + 30 + (hang - 1) * 10 + (40 + 14);
        [self MakeAnseredViewWithString:str];
        
    }
    return height;
}

+(CGFloat)MakeHeightForCellWithShowCmQuestionItemInfo:(CMGetQuestionItemInfo *)allitem{
    NSString *str = allitem.questionContent;
    CGFloat height;
    CGSize strsize = [str sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}];
    NSInteger hang = strsize.width / (SCREEN_WIDTH - 20 - 35) + 1;
    
    if (allitem.questionStatus == QuestionStatus_Close) {
        
        height = (hang * 16 + 30 + (hang - 1) * 10) + 15;
        
    }else if(allitem.questionStatus == QuestionStatus_Open){
        
        NSArray *arr = allitem.answers;
        height = ((hang * 16 + 30 + (hang - 1) * 10) + 15) +(40 + arr.count * 14 + (arr.count - 1) *22);
        
    }else if(allitem.questionStatus == QuestionStatus_Answer){
        
        height = (hang * 16 + 30 + (hang - 1) * 10) + 15 + (40 + 14);
        
    }
    return height;
}


-(UILabel *)LabelWithRect:(CGRect)rect textColor:(UIColor *)color textstr:(NSString *)str textfont:(int)font textstyle:(NSTextAlignment)aligment{
    UILabel *label = [[UILabel alloc] initWithFrame:rect];
    label.textColor = color;
    label.text = str;
    label.font = [UIFont systemFontOfSize:font];
    label.textAlignment = aligment;
    label.numberOfLines = 0;
    return label;
}

-(void)makeAnswerChoseViewWithString:(NSString *)str{
    
    CGSize strsize = [str sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}];
    NSInteger hang = strsize.width / (SCREEN_WIDTH - 20 - 35) + 1;
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(20, 30 + hang * 16 + (hang - 1) * 10, _bgvi.size.width - 20 , .5)];
    line.backgroundColor = [LCCommon getColor:@"cccccc"];
    [_bgvi addSubview:line];
    
    UIImage *notchose = [UIImage imageNamed:@"answer_notchosed"];
    UIImage *choseed  = [UIImage imageNamed:@"answer_chosed"];
    for (int i = 0; i < _answerarr.count; i ++) {
        
        _answerchose = [[UIButton alloc]initWithFrame:CGRectMake(20, STATICY + i * (SPACE + notchose.size.height), notchose.size.width, notchose.size.height)];
        [_answerchose addTarget:self action:@selector(clickToSelect:) forControlEvents:UIControlEventTouchUpInside];
        [_answerchose setImage:notchose forState:UIControlStateNormal];
        _answerchose.tag = i;
        [_bgvi addSubview:_answerchose];
        
        
        CMGetQuestionAnswerInfo *answerinfo = [_answerarr objectAtIndex:i];
        _answer = [self LabelWithRect:CGRectMake(_answerchose.origin.x + notchose.size.width + 10, ANSWERSTATICY , _bgvi.size.width - (_answerchose.origin.x + notchose.size.width + 10), 14) textColor:[LCCommon getColor:@"666666"] textstr:answerinfo.answerContent textfont:14 textstyle:NSTextAlignmentLeft];
        [_bgvi addSubview:_answer];
        
        if (self.alliteminfo.selectedAnswer.length == 0) {
            
            [_answerchose setImage:notchose forState:UIControlStateNormal];
            
        }else if([[NSString stringWithFormat:@"%ld",(long)_answerchose.tag] isEqualToString: self.alliteminfo.selectedAnswer]){
            
            [_answerchose setImage:choseed forState:UIControlStateNormal];
            
        }
    }
    
}


-(void)MakeAnseredViewWithString:(NSString *)str{
    CGSize strsize = [str sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}];
    NSInteger hang = strsize.width / (SCREEN_WIDTH - 20 - 35) + 1;
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(20, 30 + hang * 16 + (hang - 1) * 10, _bgvi.size.width - 20 , .5)];
    line.backgroundColor = [LCCommon getColor:@"cccccc"];
    [_bgvi addSubview:line];

    UIImage *chosed = [UIImage imageNamed:@"answer_chosed"];
    _answerchose = [[UIButton alloc]initWithFrame:CGRectMake(20, STATICY, chosed.size.width, chosed.size.height)];
    [_answerchose setImage:chosed forState:UIControlStateNormal];
    [_bgvi addSubview:_answerchose];
    
    CMGetQuestionAnswerInfo *answerinfo = [_answerarr objectAtIndex:[self.alliteminfo.selectedAnswer integerValue]];
    _answer = [self LabelWithRect:CGRectMake(_answerchose.origin.x + chosed.size.width + 10, _answerchose.origin.y + 2 , _bgvi.size.width - (_answerchose.origin.x + chosed.size.width + 10), 14) textColor:[LCCommon getColor:@"666666"] textstr:answerinfo.answerContent textfont:14 textstyle:NSTextAlignmentLeft];
    [_bgvi addSubview:_answer];

}

//- (void)ChoseAnswer:(UIButton *)sender
//{
//    
//    if (_delegate && [_delegate respondsToSelector:@selector(clickToSelect:)]) {
//        
//        [_delegate clickToSelect:sender];
//    }
//}


- (void)clickToSelect:(UIButton *)sender
{
    if (self.ActionPreviewClickToSelelct)
    {
        self.ActionPreviewClickToSelelct(sender);
    }
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}

@end
